package com.zilue.payment.gateway.reurndata;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zilue.payment.gateway.reurndata.enums.RequestStatusEnum;
import com.zilue.payment.gateway.manage.SignManager;
import com.zilue.payment.gateway.reurndata.enums.BusinessStatusEnum;
import com.zilue.security.util.rsa.RSAPrivateKeyStaticUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zilue on 2017/3/7.
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PublicReturn {
    private String code;
    private String msg;
    private String sub_code;
    private String sub_msg;
    private String sign;

    public PublicReturn(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public PublicReturn(RequestStatusEnum status) {
        this.code = status.getCode();
        this.msg = status.getMsg();
    }

    public void setRequestStatus(RequestStatusEnum status) {
        this.code = status.getCode();
        this.msg = status.getMsg();
    }
    public static PublicReturn createSuccessReturn(RequestStatusEnum status) {
        return new PublicReturn(status);
    }

    public static PublicReturn createFailedReturn(RequestStatusEnum status) {
        return new PublicReturn(status);
    }

    public void setBusinessStatus(BusinessStatusEnum status) {
        this.sub_code = status.getSub_code();
        this.sub_msg = status.getSub_msg();
    }

    public String getSign() {
        Map<String, String> returnMap = convertParamterMap();
        RSAPrivateKeyStaticUtil.digitalSignature(returnMap, SignManager.YUANTU_PRIVATE_KEY);
        sign = returnMap.get("sign");
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public Map<String, String> convertParamterMap() {
        Map<String, String> map = new HashMap<String, String>();
        try {
            Class clazz = this.getClass();
            while (clazz != Object.class) {
                Field[] declaredFields = clazz.getDeclaredFields();
                for (Field field : declaredFields) {
                    field.setAccessible(true);
                    String name = field.getName();
                    Object value = field.get(this);
                    if (value == null)
                        continue;
                    map.put(name, String.valueOf(value));
                }
                clazz = clazz.getSuperclass();
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }

    public void appendSub_msg(String appendMsg) {
        if (!StringUtils.isEmpty(appendMsg))
            this.sub_msg += "," + appendMsg;
    }

    public void appendMsg(String appendMsg) {
        if (!StringUtils.isEmpty(appendMsg))
            this.msg += appendMsg;
    }

}

